package com.jiaai.web.zfp.home;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.gift.api.IGiftApi;
import com.jiaai.live.api.ILiveApi;
import com.jiaai.system.api.IBannerApi;
import com.jiaai.system.api.IPayManageAPI;
import com.jiaai.system.api.ISystemConfigApi;
import com.jiaai.util.RestResultGenerator;
import com.jiaai.util.code_pack.BannerTypeCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title MainConteller
 * @since 2017/10/16
 */
@Controller
public class HomeController  {
    private Logger logger= LoggerFactory.getLogger(HomeController.class);
    @Reference
    private IBannerApi iBannerApi;
    @Reference
    private ILiveApi iLiveApi;
    @Reference
    private ISystemConfigApi systemConfigApi;
    @Reference
    private IGiftApi iGiftApi;
    @Reference
    private IPayManageAPI payManageAPI;

    /**
     * 主页面数据
     * @return
     */
    @GetMapping("/api/v1/app/home")
    @ResponseBody
    public Object home(){
        try {
            Map map = new HashMap();
            map.put("hot", iLiveApi.selectOnLives());
            map.put("recommend", iLiveApi.selectRecommendLive());
            map.put("banner", iBannerApi.getBannerRandByGroup(BannerTypeCode.HOME_PAGE, 15));
            return RestResultGenerator.genSuccessResult(map);
        }catch (Exception e){
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    @GetMapping("test")
    @ResponseBody
    public  Object test(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        logger.error("IP:++++"+ip);
        return "SB IP:"+ip;
    }

    /**
     * 系统配置, 包括礼物
     * @return
     */
    @GetMapping("/api/v1/app/systemConfig")
    @ResponseBody
    public Object getSystemConfig(){
        try {
            return RestResultGenerator.genSuccessResult(systemConfigApi.getAppSystemConfig());
        }catch (Exception e){
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    /**
     * 支付配置, 各充值档次
     * @return
     */
    @GetMapping("/api/v1/app/payConfig")
    @ResponseBody
    public Object getPayConfig(){
        return RestResultGenerator.genSuccessResult(payManageAPI.getAppList());
    }


}
